Skip to content

Load zarr DataArrays with coordinates from fsspec zip stores - #11937

Open
mvanhorn wants to merge 2 commits into
pydata:mainfrom
mvanhorn:fix/10950-zarr-fsspec-zip-dataarray
Open

mvanhorn wants to merge 2 commits into
pydata:mainfrom
mvanhorn:fix/10950-zarr-fsspec-zip-dataarray

Conversation

@mvanhorn

Copy link
Copy Markdown
Contributor

Description

Opening a Zarr archive through an fsspec zip URL or zip filesystem fails with a KeyError when reading coordinates or variables whose chunks contain only the fill value (for example, a coordinate value of 0). Loading the same archive from a local directory or directly through zarr.storage.ZipStore succeeds. Zarr omits chunks that match the fill value and expects missing store keys to return None, whereas fsspec's ZipFileSystem raises KeyError when an archive member is absent.

Because xarray eagerly reads 1D coordinate arrays to build default indexes when opening a dataset or data array, zarr.storage.FsspecStore surfaces that KeyError during open.

In xarray/backends/zarr.py, _normalize_zip_fsspec_store detects read-only zip:// URLs and FsspecStore instances backed by a zip filesystem (unwrapping AsyncFileSystemWrapper when present) and wraps the store in a cached WrapperStore subclass from _zip_missing_key_store_cls. The wrapper catches KeyError in get, get_partial_values, and get_partial (when present on WrapperStore), returning None if self._store.exists(key) is false and re-raising otherwise, while preserving with_read_only and updating close_store_on_close in _get_open_params to close newly created stores. A regression test test_load_dataarray_fsspec_zip_coordinates is added to xarray/tests/backends/test_zarr.py, and a release note is added to doc/whats-new.rst.

Running pytest -q xarray/tests/backends/test_zarr.py failed during collection with ModuleNotFoundError: No module named 'numpy'.
The documentation redirect check comparing historical doc/ pages against git ls-files doc and doc/conf.py completed with no missing pages reported.
A run with the xarray _iter_zarr_groups change reverted gave no clear result, so no test is shown to catch this bug.
The changed test xarray/tests/backends/test_zarr.py did not run here: the numpy module is not installed.

Closes #10950

Checklist

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
      No result recorded for the xarray _iter_zarr_groups change backs this box.

    • I take responsibility for any AI-generated content in my PR.

      Tools: {e.g., Claude, Codex, GitHub Copilot, ChatGPT, etc.}

AI disclosure

AI was used for assistance.

  • Tools: gemini-4-neon-high.

In `xarray/backends/zarr.py`, `_normalize_zip_fsspec_store` detects
read-only `zip://` URLs and `FsspecStore` instances backed by a zip
filesystem (unwrapping `AsyncFileSystemWrapper` when present) and wraps
the store in a cached `WrapperStore` subclass from
`_zip_missing_key_store_cls`. The wrapper catches `KeyError` in `get`,
`get_partial_values`, and `get_partial` (when present on
`WrapperStore`), returning `None` if `self._store.exists(key)` is false
and re-raising otherwise, while preserving `with_read_only` and updating
`close_store_on_close` in `_get_open_params` to close newly created
stores. A regression test `test_load_dataarray_fsspec_zip_coordinates`
is added to `xarray/tests/backends/test_zarr.py`, and a release note is
added to `doc/whats-new.rst`.

Fixes pydata#10950

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

io topic-backends topic-zarr Related to zarr storage library

Projects

None yet

Development

Successfully merging this pull request may close these issues.

xarray.load_dataarray fails when loading a DataArray with coordinates via zarr-fsspec

1 participant